package cn.melonlib.security.encryption.comp.enc.aes;

import cn.hutool.crypto.SecureUtil;
import cn.melonlib.security.encryption.comp.enc.EncryptionService;
import cn.melonlib.security.encryption.comp.enc.SecurityRegister;
import cn.melonlib.security.encryption.model.EncPackage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@ConditionalOnProperty(value = "system.security.enc.type",havingValue = "AES",matchIfMissing = true)
public class AesEncryptionService implements EncryptionService {

    @Resource
    private SecurityRegister securityRegister;

    public byte[] transEnc(byte[] input) throws Exception{
        return  SecureUtil.aes(securityRegister.getTransmissionKey().getVal())
                .encrypt(EncPackage.createEncPackage(input).toData());
    }

    public EncPackage transDec(byte[] input) throws Exception {
        return EncPackage.parseData(SecureUtil.aes(securityRegister.getTransmissionKey().getVal())
                .decrypt(input));
    }

    public byte[] storeEnc(byte[] input) throws Exception{
        return SecureUtil.aes(securityRegister.getStoreKey().getVal()).encrypt(EncPackage.createEncPackage(input).toData());
    }

    @Override
    public EncPackage storeDec(byte[] input) throws Exception{
        return EncPackage.parseData(SecureUtil.aes(securityRegister.getStoreKey().getVal()).decrypt(input));
    }
}
